package com.wbt.bambootown.ui.activity;

import android.support.design.widget.TextInputEditText;
import android.widget.Toast;

import com.gyf.barlibrary.ImmersionBar;
import com.wbt.bambootown.FrameApp;
import com.wbt.bambootown.R;
import com.wbt.bambootown.entity.User;
import com.wbt.bambootown.mvp.contract.ReplacePwdContract;
import com.wbt.bambootown.mvp.frame.MvpActivity;
import com.wbt.bambootown.mvp.frame.base.BaseView;
import com.wbt.bambootown.mvp.presenter.ReplacePwdPresenter;
import com.wbt.bambootown.utils.ActivityManagerUtil;
import com.wbt.bambootown.utils.EventCenter;
import com.wbt.bambootown.utils.SPUtils;

import butterknife.BindView;
import butterknife.OnClick;

public class ReplacePwdActivity extends MvpActivity<ReplacePwdPresenter> implements ReplacePwdContract.View {

    @BindView(R.id.edit_old_password)
    TextInputEditText mEditOldPassword;
    @BindView(R.id.edit_new_password)
    TextInputEditText mEditNewPassword;
    @BindView(R.id.edit_re_new_password)
    TextInputEditText mEditReNewPassword;

    private String oldPwd = null;
    private String newPwd = null;
    private String newRePwd = null;

    @OnClick(R.id.btn_send)
    public void onClick(){
        if(checkForm()){
            mPresenter.updatePassword(oldPwd , newPwd , newRePwd);
            showProgressDialog();
        }
    }

    @OnClick(R.id.iv_back)
    public void onBack(){
        ActivityManagerUtil.create().finishActivity();
    }

    private boolean checkForm(){
        oldPwd = mEditOldPassword.getText().toString().trim();
        newPwd = mEditNewPassword.getText().toString().trim();
        newRePwd = mEditReNewPassword.getText().toString().trim();

        boolean isPass = true;

        if(oldPwd.isEmpty()){
            mEditOldPassword.setError("请输入正确的旧密码");
            isPass = false;
        }else{
            mEditOldPassword.setError(null);
        }

        if(newPwd.isEmpty() || newPwd.length() < 6){
            mEditNewPassword.setError("请填写至少6位密码");
            isPass = false;
        }else{
            mEditNewPassword.setError(null);
        }

        if(newRePwd.isEmpty() || newRePwd.length() < 6){
            mEditReNewPassword.setError("请填写至少6位密码");
            isPass = false;
        }else if(!newRePwd.equals(newPwd)){
            mEditReNewPassword.setError("两次输入密码不一致");
            isPass = false;
        }else{
            mEditReNewPassword.setError(null);
        }

        return isPass;
    }


    @Override
    public void showError(String errorMsg) {
        dismissProgressDialog();
        Toast.makeText(this , errorMsg , Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showReplaceSuccess() {
        dismissProgressDialog();
        Toast.makeText(this , "修改成功" , Toast.LENGTH_SHORT).show();
        SPUtils.saveUser(new User( FrameApp.user.getUsername(), newPwd));
        FrameApp.loadUser();
        ActivityManagerUtil.create().finishActivity();
    }

    @Override
    protected BaseView getBaseView() {
        return this;
    }

    @Override
    public void initStatusBar(ImmersionBar immersionBar) {
        immersionBar
                .fitsSystemWindows(true)
                .statusBarColor(R.color.theme_color)
                .init();
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {

    }

    @Override
    public Object getContentView() {
        return R.layout.activity_replace_pwd;
    }

    @Override
    public boolean isApplyEventBus() {
        return false;
    }

    @Override
    public boolean isApplyButterKnife() {
        return true;
    }

    @Override
    public void onEventCome(EventCenter eventCenter) {

    }
}
